const tool = require("../../utils/tool")
const config = require("../../config")

// pages/comDetail/conDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:undefined,
    showPage:false,
    img:[],
    cl_ly_input:'',
    sat:0,
    bh_input:'',
    shFunction:false,
    userData:undefined,
    showIP:false,
  },

  uploadImgClick(e){
    e = e.currentTarget.dataset.url
    wx.showActionSheet({
      itemList: ['预览','删除'],
      success:async (e1)=>{
        switch (e1.tapIndex) {
          case 0:
            wx.previewImage({
              urls: this.data.img,
              current: e
            })
            break;
          case 1:
            let re = []
            for(let i=0;i<this.data.img.length;i++){
              if(this.data.img[i]==e)continue
              re.push(this.data.img[i])
            }
            this.setData({
              img:re
            })
            break;
        }
      }
    })
  },

  changeIP(e){
    this.setData({
      showIP:e.detail.value
    })
  },

  async bh(){
    if(this.data.bh_input==''){
      wx.showToast({
        title: '请输入驳回内容',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '是否驳回此巡查的最终处理？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/com/reject",{
            id:this.data.item.id,
            account:this.data.loginedUserInfo.account,
            content:tool.removeBreak(this.data.bh_input)
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showToast({
              title: '后端服务异常',
              icon: 'none'
            })
            return
          }
          if(re.status==0){
            wx.showToast({
              title: re.content,
              icon: 'none'
            })
            return
          }
          this.refresh()
        }
      }
    })
    

  },

  bhInputText(e){
    this.setData({
      bh_input:e.detail.value
    })
  },

  showDetailLocation(){
    wx.openLocation({
      latitude: this.data.la,
      longitude: this.data.lo,
    })
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    tool.setThemeColor(this)
    this.setData({
      sysImagURLPrefix:wx.getStorageSync('sysImagURLPrefix')
    })
    options = JSON.parse(options.data)
    this.setData({
      statusBarHeight: (parseInt(options.statusBarHeight.split('px')[0])).toString() + 'px',
      loginedUserInfo: options.loginedUserInfo,
      contentHeight: (parseInt(options.contentHeight.split('px')[0]) + 3).toString() + 'px',
      serverDownloadImageURL:config.serverURL + '/file/download'
    })
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/com/getSingle",{
      id:options.id
    })

    let re1 = await tool.requestMethod("/setting/get",{
      keyName:'sh'
    })
    this.setData({
      shFunction:re1.results
    })

    let re2 = await tool.requestMethod("/user/homeDataForComDetail",{
      account:this.data.loginedUserInfo.account
    })

    wx.hideLoading()
    if(re2.status==-2){
      wx.showToast({
        title: '后端服务异常'
      })
      return
    }
    this.setData({
      userData:re2.results
    })
    if(re1.status==-2){
      wx.showToast({
        title: '后端服务异常'
      })
      return
    }
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    if(re.results==undefined){
      wx.showModal({
        title: '提示',
        content: '此巡查不存在，可能已被删除',
        showCancel: false,
        complete: (res) => {
          wx.navigateBack()
        }
      })
      return
    }
    try {
      re.results.content3 = JSON.parse(re.results.content3)
      re.results.account3 = JSON.parse(re.results.account3)
    } catch (error) {
      
    }
    try {
      this.setData({
        la:re.results.lalo[0],
        lo:re.results.lalo[1],
      })
    } catch (error) {
      
    }
    this.setData({
      item:re.results,
    })
    this.setData({
      userPer:tool.checkUserPer(re2.results,this.data.item),
      showPage:true
    })
    console.log(this.data.userPer)
  },

  async refresh(){
    wx.setStorageSync('refreshComList', 'true')
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/com/getSingle",{
      id:this.data.item.id
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    if(re.results==undefined){
      wx.showModal({
        title: '提示',
        content: '此巡查不存在，可能已被删除',
        showCancel: false,
        complete: (res) => {
          wx.navigateBack()
        }
      })
      return
    }
    this.setData({
      item:re.results
    })
    this.setData({
      userPer:tool.checkUserPer(this.data.userData,this.data.item)
    })
  },

  async submitSat(){
    if(this.data.sat==0)return
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/com/sat",{
      id:this.data.item.id,
      sat:this.data.sat
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    wx.setStorageSync('refreshComList', 'true')
        wx.showLoading({ mask:true,
          title: '请稍候',
        })
        re = await tool.requestMethod("/com/getSingle",{
          id:this.data.item.id
        })
        wx.hideLoading()
        if(re.status==-2){
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          wx.navigateBack()
          return
        }
        if(re.results==undefined){
          wx.showModal({
            title: '提示',
            content: '此巡查不存在，可能已被删除',
            showCancel: false,
            complete: (res) => {
              wx.navigateBack()
            }
          })
          return
        }
        this.setData({
          item:re.results
        })
        wx.showModal({
          title: '提示',
          content: '评价成功',
          showCancel: false
        })
  },

  changeSat(e){
    e = e.currentTarget.dataset.sat
    this.setData({
      sat:e
    })
  },

  async wd(){
    wx.showModal({
      title: '提示',
      content: '是否撤回此巡查？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/com/withdraw",{
            id:this.data.item.id
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          if(re.status==0){
            wx.showModal({
              title: '提示',
              content: re.content,
              showCancel: false
            })
            return
          }
          wx.setStorageSync('refreshComList', 'true')
          wx.navigateBack()
        }
      }
    })
    
  },

  async sc(){
    wx.showModal({
      title: '提示',
      content: '是否删除？',
      complete: async  (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/com/delete",{
            account:this.data.loginedUserInfo.account,
            id:this.data.item.id
          })
          wx.hideLoading()
          if(re.status==-2){
            tool.showToast('后端服务异常')
            return
          }
          if(re.status==0){
            tool.showToast(re.content)
            return
          }
          console.log(re)
          wx.navigateBack()
          tool.showToast('删除成功')
        }
      }
    })
    
  },

  async makeSinglePhoneCall(e){
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/user/phoneNumber",{
      account:e.currentTarget.dataset.account
    })
    wx.hideLoading()
    if(re.status==-2){
      tool.showToast('后端服务异常')
      return
    }
    if(re.status==0){
      tool.showToast(re.content)
      return
    }
    wx.makePhoneCall({
      phoneNumber: re.results,
    })
  },

  async makePhoneCall(){
    tool.showComPhoneNumberList(this.data.statusBarHeight,this.data.contentHeight,this.data.loginedUserInfo,this.data.item.id)
    // e = e.currentTarget.dataset.account
    // wx.showLoading({ mask:true,
    //   title: '请稍候',
    // })
    // let re = await tool.requestMethod("/user/phoneNumber",{
    //   account:e
    // })
    // wx.hideLoading()
    // if(re.status==-2){
    //   wx.showModal({
    //     title: '提示',
    //     content: '后端服务异常',
    //     showCancel: false
    //   })
    //   return
    // }
    // if(re.results.length!=11){
    //   wx.showModal({
    //     title: '提示',
    //     content: '当前帐号未设置电话号码',
    //     showCancel: false
    //   })
    //   return
    // }
    // wx.makePhoneCall({
    //   phoneNumber: re.results,
    // })
  },

  async submit_wcl(){
    if(this.data.cl_ly_input==''){
      wx.showModal({
        title: '提示',
        content: '请输入留言',
        showCancel: false
      })
      return
    }
    if(this.data.img.length==0){
      wx.showModal({
        title: '提示',
        content: '请上传图片',
        showCancel: false
      })
      return
    }
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let imgupld = []
    for(let i=0;i<this.data.img.length;i++){
      let re = await tool.uploadFile(this.data.img[i])
      if(re){
        imgupld.push(re)
      }else{
        continue
      }
    }
    let re = await tool.requestMethod("/com/deal",{
      id:this.data.item.id,
      content2:tool.removeBreak(this.data.cl_ly_input),
      image2:imgupld,
      account2:this.data.loginedUserInfo.account,
      isDeal:0
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    if(re.status==-1){
      wx.showModal({
        title: '提示',
        content: '此巡查已被删除',
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    wx.showModal({
      title: '提示',
      content: '处理成功',
      showCancel: false,
      success:async (res)=>{
        wx.setStorageSync('refreshComList', 'true')
        wx.showLoading({ mask:true,
          title: '请稍候',
        })
        let re = await tool.requestMethod("/com/getSingle",{
          id:this.data.item.id
        })
        wx.hideLoading()
        if(re.status==-2){
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          wx.navigateBack()
          return
        }
        if(re.results==undefined){
          wx.showModal({
            title: '提示',
            content: '此巡查不存在，可能已被删除',
            showCancel: false,
            complete: (res) => {
              wx.navigateBack()
            }
          })
          return
        }
        this.setData({
          item:re.results
        })
      }
    })

  },

  async submit_cl(){
    if(this.data.cl_ly_input==''){
      wx.showModal({
        title: '提示',
        content: '请输入留言',
        showCancel: false
      })
      return
    }
    if(this.data.img.length==0){
      wx.showModal({
        title: '提示',
        content: '请上传图片',
        showCancel: false
      })
      return
    }
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let imgupld = []
    for(let i=0;i<this.data.img.length;i++){
      let re = await tool.uploadFile(this.data.img[i])
      if(re){
        imgupld.push(re)
      }else{
        continue
      }
    }
    let re = await tool.requestMethod("/com/deal",{
      id:this.data.item.id,
      content2:tool.removeBreak(this.data.cl_ly_input),
      image2:imgupld,
      account2:this.data.loginedUserInfo.account,
      isDeal:1
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    if(re.status==-1){
      wx.showModal({
        title: '提示',
        content: '此巡查已被删除',
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    wx.showModal({
      title: '提示',
      content: '处理成功',
      showCancel: false,
      success:async (res)=>{
        wx.setStorageSync('refreshComList', 'true')
        wx.showLoading({ mask:true,
          title: '请稍候',
        })
        let re = await tool.requestMethod("/com/getSingle",{
          id:this.data.item.id
        })
        wx.hideLoading()
        if(re.status==-2){
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          wx.navigateBack()
          return
        }
        if(re.results==undefined){
          wx.showModal({
            title: '提示',
            content: '此巡查不存在，可能已被删除',
            showCancel: false,
            complete: (res) => {
              wx.navigateBack()
            }
          })
          return
        }
        this.setData({
          item:re.results
        })
      }
    })

  },

  cl_ly_input(e){
    e = e.detail.value
    this.setData({
      cl_ly_input:e
    })
  },

  deleteImage(e){
    e = e.currentTarget.dataset.item
    let re = []
    for(let i=0;i<this.data.img.length;i++){
      if(this.data.img[i]==e){
        continue
      }
      re.push(this.data.img[i])
    }
    this.setData({
      img:re
    })
  },

  showImageViewerLocal(e){
    wx.previewImage({
      urls: e.currentTarget.dataset.arr,
      current: e.currentTarget.dataset.current
    })
  },

  showImageViewer(e){
    for(let i=0;i<e.currentTarget.dataset.arr.length;i++){
      e.currentTarget.dataset.arr[i] = config.serverURL + '/file/download/' + e.currentTarget.dataset.arr[i]
    }
    console.log(e)
    wx.previewImage({
      urls: e.currentTarget.dataset.arr,
      current: config.serverURL + '/file/download/' + e.currentTarget.dataset.current
    })
  },

  async uploadFile(){
    wx.chooseImage({  
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {  
        const tempFilePaths = res.tempFilePaths;  
        let re = this.data.img
        re.push(tempFilePaths[0])
        this.setData({
          img:re
        })
      }  
    })
  },

  back() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})